﻿/**
* @private
* @license	GNU GPLv2
*
* Copyright (C) 2007  Polly Jex.
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
* MA  02110-1301, USA.
*
* @see	http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/

package com.kirika.utils {

	/**
	 * @import
	 */
	
	import com.kirika.core.CoreStatic;
	import com.kirika.data.tda.StringIterator;
	import com.kirika.data.types.UID;
	import com.kirika.utils.TypeUtil;
	import flash.display.LoaderInfo;
	import flash.external.ExternalInterface;
	
	/**
	 * Utilidades para la manipulacion de URI's.
	 * @author	Polly Jex (pollyjex@emc2zen.com.ar)
	 */
	public class UriUtil extends CoreStatic {
		
		/**
		 * Lista de carcateres que no debe poseer una direccion de email.
		 */
		private static const EMAIL_CHAR_DISALLOWED:String = new String( "()<>,;:\\\"[] `~!#$%^&*+={}|/?'" );
		
		/**
		 * Retona la URI del documento.
		 * @return	String
		 */
		public static function get url():String {			
			
			return LoaderInfo.getLoaderInfoByDefinition( UriUtil ).url;		
			
		}
		
		/**
		 * Retona la URI del documento utilizando de interprete JavaScript.
		 * @return	String
		 */
		public static function get urlForJS():String {	
			
			return ExternalInterface.call( "window.location.href.toString" );	
			
		}
			
		/**
		 * Resuelve y retorna una URI que elimina el problema de cache.
		 * @param	uri	URI a resolver.
		 * @return	String
		 */
		public static function resolveAntiCache( uri:String = null ):String {
			
			if( TypeUtil.isNull( uri ) || uri.indexOf( "?" ) == -1 ){
				return "?flash_ac=" + UID.create();
			}else{
				return uri;
			}
			
		}
		
		/**
		 * Verifica si la direccion de email es correcta.
		 * @param	value	Direccion de email a verifica.
		 * @return	Boolean
		 */
		public static function isEmail( value:String ):Boolean {
			
			var i:StringIterator = new StringIterator( value );
			
			while( i.hasNext() ){
				if( EMAIL_CHAR_DISALLOWED.indexOf( i.next() ) != -1 ){
					return false;
				}
			}
			
			var rexString:RegExp = new RegExp( "^([a-zA-Z0-9]+)([a-zA-Z0-9-_\.]*)@([a-zA-Z0-9-]+)[\.]([a-zA-Z]{2,4}[\.a-zA-Z]{0,4})$" );
			return new Boolean( ! TypeUtil.isNull( rexString.exec( value ) ) );			
			
		}
		
		/**
		 * Verifica si el esquema de la uri es "file:"
		 * @param	uri		Uri a verificar.
		 * @return	Boolean
		 */
		public static function isFile( uri:String ):Boolean {
			
			return ( getScheme( uri ) == "file" ) ? true : false;
			
		}
		
		/**
		 * Verifica si el esquema de la uri es "http:"
		 * @param	uri		Uri a verificar.
		 * @return	Boolean
		 */
		public static function isHttp( uri:String ):Boolean {
			
			return ( getScheme( uri ) == "http" ) ? true : false;
			
		}
		
		/**
		 * Verifica si el esquema de la uri es "https:"
		 * @param	uri		Uri a verificar.
		 * @return	Boolean
		 */
		public static function isHttps( uri:String ):Boolean {
			
			return ( getScheme( uri ) == "https" ) ? true : false;
			
		}	
		
		/**
		 * Verifica si el esquema de la uri y retorna una cadena con el valor.
		 * @param	uri		Uri a verificar.
		 * @return	Boolean
		 */
		private static function getScheme( uri:String ):String {
			
			var end:int = uri.search( ":" );
			
			if( end == -1 ){
				return null;
			}
			
			return new String( uri.substr( 0, end ) ).toLowerCase();
			
		}
		
	}
	
}
